package com.lc.qianrenshiye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.adapter.MessageCenterAdapter;
import com.lc.qianrenshiye.conn.Conn;
import com.lc.qianrenshiye.conn.MessageListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/24.
 */
public class MessageCenterActivity extends BaseActivity{

    @BoundView(R.id.message_center_lv) private ListView message_center_lv;
    @BoundView(R.id.message_center_line_rl) private RelativeLayout message_center_line_rl;

    private List<MessageListGet.MessageEntity> lists = new ArrayList<>();

    private MessageCenterAdapter adapter;

    private MessageListGet messageListGet = new MessageListGet(new AsyCallBack<MessageListGet.MessageListEntity>(){
        @Override
        public void onSuccess(String toast, int type, MessageListGet.MessageListEntity messageListEntity) throws Exception {

            lists.addAll(messageListEntity.messageEntityList);
            adapter.notifyDataSetChanged();

            Log.e("lists.size()", lists.size() + "");


            BaseApplication.BasePreferences.saveMessageCount(lists.size());

            message_center_line_rl.setVisibility(View.VISIBLE);

            BusinessCenterActivity.RefreshTitleAndHead refreshTitleAndHead = (BusinessCenterActivity.RefreshTitleAndHead) getAppCallBack(BusinessCenterActivity.class);

            refreshTitleAndHead.refreshMessage(lists.size());


        }

        @Override
        public void onFail(String toast, int type) throws Exception {


        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_center);

        setBackTrue();

        setTitleName(getString(R.string.message_center_title_text));


        message_center_lv.setAdapter(adapter = new MessageCenterAdapter(context, lists));


        message_center_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Bundle bundle = new Bundle();

                bundle.putString("url", Conn.SERVICE + Conn.NEWS_NEWS_CON + "?id=" + lists.get(position).id);
                bundle.putString("title", getString(R.string.message_detail_title_text));

                startActivity(new Intent(MessageCenterActivity.this, MWebActivity.class).putExtras(bundle));

//                startActivity(new Intent(MessageCenterActivity.this, MessageDetailActivity.class));

            }
        });

        messageListGet.execute(this);

    }
}
